<?php

namespace app\crm\controller\contract;
use app\crm\model\contract\Contract as ContractModel;
use app\crm\model\contract\Product as ProductModel;
use app\crm\model\product\Course as ProductCourse;
use app\crm\validate\contract\Contract as ContractContract;
use think\Controller;
use think\Db;
use think\facade\Session as FacadeSession;

/**
 * @Descripttion: 全部合同界面
 * @Date: 2022-11-27 15:25:16
 */
class Contract  extends Controller
{
    public function index()
    {
        FacadeSession::delete('id');
        if(Request()->param('searchParams')){
            $result=Request()->get('searchParams');
            // dump($result);
            $arr=[];
            foreach($result as $k => $v){
                if(!$v){
                    unset($result[$k]);
                }
                else{
                    $arr[$k]=$v;
                }
            }
            $data = ContractModel::where($arr)->select();
            return  json(["code" => 0, "total" => count($data), "data"  => $data]);
        } else  if(Request()->isGet() && Request()->isAjax()) {
            $data = ContractModel::with('ourclients,product,sale,principal')->select();
            // $data = ProductModel::with('contract')-> ();
            // dump($data);
            return  json(["code" => 0, "data" => $data]);
        }
        return $this->fetch();
    }
     
    /**
     * @Descripttion: 合同添加
     * @Date: 2022-11-27 15:25:39a
     */
    public function add()
    {
        if (Request()->isGet() && Request()->isAjax()){
            $id = Request()->get('id');
            // dump($id);
            if (isset($id)) {
                FacadeSession::set('id',$id);
            }
            $id=FacadeSession::get('id',$id);
            if ($data = ProductCourse::with('major,campus')->all($id ?? 0)) {
            return json(["code" => 0, "total" => count($data), "data" => $data]);

            }else{
            exception('选择商品的数据存在问题', 10006);
            return json(['errcode' => 0, 'errmsg' => '数据异常请稍后重试']);
            }
            

        }
        if(Request()->isPost() && Request()->isAjax()){
            $arr=Request()->param();
            if(array_key_exists('confirm',$arr)){
            }else{
            $data=$arr['tract'];
            $ContractIndex = new ContractContract();
            if (!$ContractIndex->check($data)) {
                return json(['errcode' => 2, 'errmsg' => $ContractIndex->getError()]);
            } else {
                    if (is_object($contract = ContractModel::create($data))) {
                        $val = $arr['commodity'];
                        foreach ($val as $k => $v) {
                            $val[$k]['contract_id'] = $contract->id;
                        }
                       if(Db::name('crm_contract_product')->insertAll($val) > 0){
                        return json(['errcode' => 1, 'errmsg' => '添加成功']);                           
                        }else{
                            exception('添加课程存在问题', 10006);
                            return json(['errcode' => 0, 'errmsg' => '数据异常添加失败请稍后重试']);
                        }
                    }
            }
        }
    }
         $info=ContractModel::info();
         $business=ContractModel::business();
         $salesman=ContractModel::salesman();
         $this->assign(['info'=>$info,'business'=>$business,'salesman'=>$salesman]);
        return $this->fetch();
    }
/**
 * @Descripttion: 合同编辑
 * @Date: 2022-11-27 16:10:30
 */    

    public function edit()
    {
        if (Request()->isGet()) {
            $id = Request()->get();
            $row = ContractModel::with('ourclients,sale,principal,cllist')->where('id',$id['id'])->select();
            $this->assign(['row' => $row]);
        }
        if (Request()->isGet() && Request()->isAjax()){
            $id = Request()->get('id');
            // dump($id);
            if (isset($id)) {
                FacadeSession::set('id',$id);
            }
            $id=FacadeSession::get('id',$id);
            if ($data = ProductCourse::with('major,campus')->all($id ?? 0)) {
            return json(["code" => 0, "total" => count($data), "data" => $data]);

            }else{
            exception('选择商品的数据存在问题', 10006);
            return json(['errcode' => 0, 'errmsg' => '数据异常请稍后重试']);
            }
            

        }
        if(Request()->isPost() && Request()->isAjax()){
            $arr=Request()->param();
            if(array_key_exists('confirm',$arr)){
            }else{
            $data=$arr['tract'];
                    if (is_object($contract = ContractModel::updata($data))) {
                        $val = $arr['commodity'];
                        foreach ($val as $k => $v) {
                            $val[$k]['contract_id'] = $contract->id;
                        }
                       if(Db::name('crm_contract_product')->updateAll($val) > 0){
                        return json(['errcode' => 1, 'errmsg' => '修改成功']);                           
                        }else{
                            exception('添加课程存在问题', 10006);
                            return json(['errcode' => 0, 'errmsg' => '数据异常添加失败请稍后重试']);
                        }
                    // }
            }
        }
    }

        $info=ContractModel::info();
        $business=ContractModel::business();
        $salesman=ContractModel::salesman();
        $this->assign(['info'=>$info,'business'=>$business,'salesman'=>$salesman]);
       return $this->fetch();
    }

    /**
     * @Descripttion: 回收站
     * @Date: 2022-11-30 18:39:14
     */
    public function recycle()
    {  
        if(Request()->isGet() && Request()->isAjax()){
            $data = ContractModel::where('recycle',1)->select();
                return  json(["code" => 0, "total" => count($data), "data" => $data]);
        }
        return $this->fetch();

    }
    /**
     * @Descripttion: 删除
     * @Date: 2022-11-30 19:24:53
     */    
    public function delete()
    {  
        if (Request()->isGet()) {
            $id = Request()->get('id');
            if (isset($id)) {
            $user = ContractModel::get($id);
            $user->recycle    = '1';
           if($user->save() == true){
            return json(['errcode' => 1, 'errmsg' => '删除成功']);
           }
            }
        }
    /**
     * @Descripttion: 还原
     * @Date: 2022-12-01 14:17:22
     */
    }
    public function restore()
    {

        if (Request()->ispost()) {
            $id = Request()->post();
            if (isset($id)) {
            $arr=explode(',', $id['id']);
            array_pop($arr);
            $user = new ContractModel;
            $list = [];
                foreach($arr as $k => $v){
                    $list[] = ['id'=>$v, 'recycle'=>'0'];
                }
           $user->saveAll($list);
            return json(['errcode' => 1, 'errmsg' => '还原成功']);                  
           
        }
    }

    }
    /**
     * @Descripttion:销毁
     * @Date: 2022-12-01 14:17:12
     */    
    public function destroy()
    {
        if (Request()->ispost()) {
            $id = Request()->post();
            if (isset($id['id'])) {
              if(ContractModel::destroy($id['id'])){
            return json(['errcode' => 1, 'errmsg' => '销毁成功']);
            } 
        }
    }
    
    }
    /**
     * @Descripttion: 审核日志
     * @Date: 2022-12-01 15:06:30
     */    
    public function audit()
    {

        return $this->fetch();
        
    }
    /**
     * @Descripttion: 商品添加
     * @Date: 2022-12-06 11:01:02
     */    
    public function commodity()
    {
        if ($this->request->isAjax()) {
            $list = ProductCourse::with('major,campus')->select();
            foreach ($list as $value) {
                $value['unit_name'] = $value['unit_name']; // 转换单位
                $value['minute_hour'] = $value['minute_hour'] . '分钟';
                
            }
            // dump($list);
            return json(['code' => 0, 'msg' => 'success', 'count' => ProductCourse::count(), 'data' => $list]);
        }
        return $this->fetch();
    
    
    }
    /**
     * @Descripttion: 商机添加
     * @Date: 2022-12-06 11:00:27
     */    
    public function business(){
        if (Request()->isGet() && Request()->isAjax()) {
        $data = Db::table('crm_business')
            ->alias('a')
            ->join(['crm_client' => 'w'], 'a.client_id = w.id')
            ->join(['yd_crm_admin' => 'c'], 'a.sale_id = c.id')
            ->join(['yd_crm_admin' => 'd'], 'a.principal = d.id')
            ->field('a.id,a.course_id, c.account as sale,w.name as client,a.client_id ,a.name,d.account as principal,a.money,a.sale_id,a.principal as principal_id,a.remark,a.predict_time,a.status')
            ->select();
            // dump($data);
            return  json(["code" => 0, "total" => count($data), "data" => $data]); 
        }
        return $this->fetch();
            
    }
    /**
     * @Descripttion: 我的合同
     * @Date: 2022-12-01 18:05:03
     */    
    public function my()
    {
        
        if(Request()->isGet() && Request()->isAjax()){
            $data = ContractModel::where([])->select();
                return  json(["code" => 0, "total" => count($data), "data" => $data]);
        }
       return $this->fetch();
    }
    // public function stamp()
    // {
    //     Db::table('crm_contract')
    //     ->alias('a')
    //     ->leftJoin('crm_contract_product w','a.id = w.contract_id')
    //     ->find();
    //    return $this->fetch('crm/contract.contract/index');

    // }

}